package com.teamsun.service;

import com.teamsun.datasource.DynamicDataSource;
import com.teamsun.entity.AccessIn;
import com.teamsun.entity.DBCfg;
import com.teamsun.mapper.AccessInMapper;
import com.teamsun.mapper.DBCfgMapper;
import com.teamsun.mapper.SaveRptInfoMapper;
import com.teamsun.mapper.SaveRptInfoMySQLMapper;
import com.teamsun.rptcfg.BaseRpt;
import com.teamsun.util.Constants;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>保存报表服务实现类<p/>
 */
@Service
public class SaveRptServiceImpl implements ISaveRptService {
    private static  final Logger LOGGER = Logger.getLogger(SaveRptServiceImpl.class);

    @Autowired
    private DBCfgMapper dbCfgMapper;

    @Autowired
    private AccessInMapper accessInMapper;

    @Autowired
    private SaveRptInfoMapper saveRptInfoMapper;

    @Autowired
    private SaveRptInfoMySQLMapper saveRptInfoMySQLMapper;

    @Autowired
    private DynamicDataSource dynamicDataSource;

    /**
     * 查询报表工具数据源配置信息
     * @return
     */
    @Override
    public Map<String, Object> queryCfg() {
        Map<String, Object> params = new HashMap<String, Object>();
        List<DBCfg> dbCfgs = dbCfgMapper.queryDBCfg(params);
        List<AccessIn> accessIns = accessInMapper.queryAccessIn(params);

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("dbCfgs", dbCfgs);
        result.put("accessIns", accessIns);
        return result;
    }

    /**
     * 根据数据源ID查询数据源配置信息
     * @param dbId 数据源ID
     * @return
     */
    @Override
    public DBCfg queryDBCfgById(String dbId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("dbId", dbId);
        List<DBCfg> dbCfgs = dbCfgMapper.queryDBCfg(param);
        DBCfg dbCfg = null;
        if(dbCfgs != null && dbCfgs.size() > 0) {
            dbCfg = dbCfgs.get(0);
        }
        return dbCfg;
    }

    /**
     * 保存报表配置信息
     * @param rptInfo 报表信息对象
     */
    @Override
    public void saveRptInfo(BaseRpt rptInfo) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("rptID", rptInfo.getRptId());  // 报表ID
        param.put("rptName", rptInfo.getRptName());  // 报表名称
        param.put("rptDBId", rptInfo.getDataSource());  // 报表数据源ID
        param.put("rptHeader", rptInfo.getRptHeader());  // 报表表头(JSON字符串)
        param.put("rptToken", rptInfo.getRptToken());  // 接入系统Token
        param.put("accessSys", rptInfo.getAccessSysCode());  // 接入系统
        param.put("createUser", rptInfo.getCreateUser());  // 报表创建人
        param.put("createDate", new Date());  // 报表创建日期

        String dbType = dynamicDataSource.getMasterDBType();
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            saveRptInfoMySQLMapper.saveRptInfo(param);
        } else {
            saveRptInfoMapper.saveRptInfo(param);
        }
    }

    /**
     * 查询所有的报表配置信息
     * @return
     */
    @Override
    public List<BaseRpt> queryAllRptCfg() {
        List<BaseRpt> baseRpts = saveRptInfoMapper.queryRptInfo(new HashMap<String, Object>());
        return baseRpts;
    }

    /**
     * 根据报表ID查询报表配置信息
     * @param rptId 报表ID
     * @return
     */
    @Override
    public BaseRpt queryRptCfgById(String rptId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("rptId", rptId);
        List<BaseRpt> baseRpts = saveRptInfoMapper.queryRptInfo(param);
        BaseRpt baseRpt = null;
        if(baseRpts != null && baseRpts.size() > 0) {
            baseRpt = baseRpts.get(0);
        }
        return baseRpt;
    }

    /**
     * 修改报表配置信息
     * @param editHead  是否修改表头 0-不修改 1-修改
     * @param baseRpt 需修改的信息
     */
    @Override
    public void updateRptCfg(String editHead, BaseRpt baseRpt) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("rptId", baseRpt.getRptId());
        params.put("rptName", baseRpt.getRptName());
        params.put("rptDbId", baseRpt.getDataSource());
        params.put("sysCode", baseRpt.getAccessSysCode());
        params.put("token", baseRpt.getRptToken());
        if(Constants.YES.equals(editHead)) {
            params.put("rptHeader", baseRpt.getRptHeader());
        }

        saveRptInfoMapper.updateRptCfg(params);
    }
}
